package com.baselib.android.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.baselib.android.utils.Logger;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseRAadapter <T1, T2 extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T2>{

    protected List<T1> data;

    public BaseRAadapter() {
        data = new ArrayList<>();
    }

    public void replace(T1 t, int position) {
        if (t == null) {
            return;
        }
        data.set(position, t);
        notifyDataSetChanged();
    }

    public void appData(T1 t1) {
        if (t1 == null) {
            return;
        }
        data.add(t1);
        notifyDataSetChanged();
    }

    public void appHeadData(T1 t1) {
        if (t1 == null) {
            return;
        }
        data.add(0, t1);
        notifyDataSetChanged();
    }

    public void appHeadData(int index, T1 t1) {
        if (t1 == null) {
            return;
        }
        data.add(index, t1);
        notifyDataSetChanged();
    }


    public void appArrayData(List<T1> t1s) {
        if (t1s == null || t1s.size() == 0) {
            return;
        }
        data.addAll(t1s);
        notifyDataSetChanged();
    }

    public List<T1> getData() {
        return data;
    }

    public void setData(List<T1> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void appendPreData(List<T1> data) {
        this.data.addAll(0, data);
        notifyItemRangeInserted(0, data.size());
    }

    public void appendLastData(List<T1> data) {


        int index = this.data.size() == 0 ? 0 : (this.data.size() - 1);
        this.data.addAll(index, data);
        notifyDataSetChanged();
    }

    public T1 getItem(int position) {
        return data.get(position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    protected OnItemClickListener itemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    void bindClickLiener(View view, final int position) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(view, position);
                }
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.onItemLongClick(view, position);
                }
                return true;
            }
        });
    }


    @NonNull
    @Override
    public T2 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(getLayoutId(i), viewGroup, false);
        T2 holder = creatHolder(view);
        return holder;
    }

    protected abstract T2 creatHolder(View view);

    protected abstract int getLayoutId(int position);

    protected abstract void bindData(int position, T1 item, T2 holder);

    @Override
    public void onBindViewHolder(@NonNull T2 viewHolder, int i) {
        try {
            T1 imChatVo = getItem(i);
            bindClickLiener(viewHolder.itemView, i);
            bindData(i, imChatVo, viewHolder);
        } catch (Exception e) {
            Logger.e(e);
//            ToastTool.show(e.getLocalizedMessage());
        }

    }
}
